package com.cy.pj.sys.controller;

import com.cy.pj.common.pojo.JsonResult;
import com.cy.pj.sys.pojo.HouseUser;
import com.cy.pj.sys.pojo.SysUser;
import com.cy.pj.sys.service.ServiceUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/")
public class UserController {
    @Autowired
    private ServiceUser serviceUser;


    //根据客户username查询客户信息
    @RequestMapping("/findUserByUserName")

    public JsonResult findUserByUserName(String username){
        List<SysUser> userByUserName = serviceUser.findUserByUserName(username);
        return new JsonResult(userByUserName);
    }
    //基于username更改用户信息
    @PostMapping("doUpdateUser")
    public JsonResult doUpdateUser(SysUser sysUser){
        serviceUser.updateUser(sysUser);
        return new JsonResult("Update Ok");
    }

    //注册
    @PostMapping("/doSaveUser")
    public JsonResult doSaveUser(SysUser users) {
        serviceUser.insertUser(users);
        return new JsonResult("注册成功");
    }

    //登录
    @RequestMapping("/doSelectUser")
    public JsonResult doSelectUser(@RequestParam String username,@RequestParam String password){
        UsernamePasswordToken token =
                new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        return new JsonResult("login ok");

    }
    //修改密码
    @RequestMapping("/doUpdatePassword")
    public JsonResult doUpdatePassword(
            String pwd,
            String newPwd,
            String cfgPwd){
        serviceUser.updatePassword(pwd, newPwd, cfgPwd);
        return new JsonResult("update ok");
    }
}
